package com.glory.gloryquickui.activity

import android.os.Bundle
import android.view.View
import com.glory.gloryquickui.R
import com.glory.gloryquickui.base.BaseActivity
import com.glory.gloryquickui.viewmodel.LoginViewModel
import com.glory.hiwork.mvvm.bean.ToastMessageBean
import kotlinx.android.synthetic.main.activity_login.*
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions

open class LoginActivity : BaseActivity<LoginViewModel>(), View.OnClickListener , EasyPermissions.PermissionCallbacks{

    override fun layoutId(): Int {
        return R.layout.activity_login
    }


    override fun initView(savedInstanceState: Bundle?) {
        viewModel.initUser(this)
        btnLogin.setOnClickListener(this)
        ivSetting.setOnClickListener(this)
    }

    override fun initData() {
//        viewModel.defUI.callObserve.observe(this, {
//            startActivity(ListActivity::class.java)
//        })
    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.btnLogin ->{
                val userName = etUser.text.toString()
                if (userName.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请输入用户名",false)
                    return
                }
                val password = etPassword.text.toString()
                if (password.isEmpty()){
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请输入密码",false)
                    return
                }
                viewModel.login(userName,password)
            }
            R.id.ivSetting -> startActivity(SettingActivity::class.java)
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>?) {

    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>?) {
        viewModel.defUI.toastEvent.value = ToastMessageBean("用户授权失败", false)
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms!!)) {
            AppSettingsDialog.Builder(this).build().show()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }
}